package com.txd.mofa.wode.aty;

import android.Manifest;
import android.content.Intent;

import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.baseframework.util.OnPermissionResponseListener;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.kongzue.takephoto.TakePhotoUtil;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.txd.mofa.R;
import com.txd.mofa.base.BaseAty;
import com.txd.mofa.net.NetUtils;
import com.txd.mofa.utils.SavePhoto;
import com.txd.mofa.view.XuanzeDialog;
import com.wang.avi.AVLoadingIndicatorView;

import java.io.File;
import java.text.ParseException;
import java.util.Map;

@Layout(R.layout.aty_touxiang)
public class TouxiangAty extends BaseAty {

    private ImageView iv_back, iv, iv_tou;
    private AVLoadingIndicatorView avi;

    @Override
    public void initViews() {
        super.initViews();
        avi = findViewById(R.id.avi);
        iv_back = findViewById(R.id.iv_back);
        iv = findViewById(R.id.iv);
        iv_tou = findViewById(R.id.iv_tou);

        Callback();
//        avi.show();
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    public void initDatas(JumpParameter paramer) {
        Glide.with(me).load(paramer.getString("url")).into(iv_tou);
//        avi.hide();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        TakePhotoUtil.getInstance(me).onActivityResult(requestCode, resultCode, data);
    }

    private void Callback() {

        TakePhotoUtil.getInstance(me).setReturnPhoto(new TakePhotoUtil.ReturnPhoto() {
            @Override
            public void onGetPhotos(final String[] selectImagePaths) {
                String selectImagePath = selectImagePaths[0];



                HttpRequest.POST(me, replaceHeadPic, new Parameter()
                        .add("token",token)
                        .add("img",new File(selectImagePath)), new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        if (error == null) {
                            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                            Toast.makeText(me, map.get("message"), Toast.LENGTH_SHORT).show();
                            if (map.get("code").equals("1")){
                                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                                Glide.with(me).load(selectImagePath).into(iv_tou);
//                                avi.hide();
                            }
                        }else {
                            toast("网络异常");
                        }
                    }
                });
            }

            @Override
            public void onError(Exception e) {
                Toast.makeText(me, "发生错误，请在Logcat查看详情", Toast.LENGTH_SHORT);
                e.printStackTrace();
            }
        });
    }

    @Override
    public void setEvents() {

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                XuanzeDialog xuanzeDialog = new XuanzeDialog(me, new XuanzeDialog.SignListener() {
                    @Override
                    public void xiangji() {
                        requestPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, new OnPermissionResponseListener() {
                            @Override
                            public void onSuccess(String[] permissions) {
                                TakePhotoUtil.getInstance(me).doOpenCamera();
                            }

                            @Override
                            public void onFail() {

                            }
                        });
                    }

                    @Override
                    public void xiangce() {
                        requestPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, new OnPermissionResponseListener() {
                            @Override
                            public void onSuccess(String[] permissions) {
                                TakePhotoUtil.getInstance(me).doOpenGallery();
                            }

                            @Override
                            public void onFail() {

                            }
                        });
                    }

                    @Override
                    public void baocun() {
                        requestPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.READ_EXTERNAL_STORAGE}, new OnPermissionResponseListener() {
                            @Override
                            public void onSuccess(String[] permissions) {

                                try {
                                    SavePhoto savePhoto = new SavePhoto(me);
                                    String s = savePhoto.SaveBitmapFromView1(iv_tou);
                                    Glide.with(me).load(s).into(iv_tou);
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onFail() {

                            }
                        });
                    }
                });
                xuanzeDialog.show();
            }
        });

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
